package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/member")
@RestController
@Slf4j
public class MemberController {

    @Reference
    private MemberService memberService;

    /**
     * 查询套餐预约统计报告
     * 返回值数据格式
     * {
     * "data":{
     * "setmealNames":["套餐1","套餐2","套餐3"],
     * "setmealCount":[
     * {"name":"套餐1","value":10},
     * {"name":"套餐2","value":30},
     * {"name":"套餐3","value":25}
     * ]
     * },
     * "flag":true,
     * "message":"获取套餐统计数据成功"
     * }
     *
     * @return
     */
    @GetMapping("/getmembergender")
    public Result getmembergender() {

        //获取数据
        List<Map<String, Object>> memberGender = memberService.groupByGender();

        log.info(":{}",memberGender);
        List<String> setmealNames = new ArrayList<>();
        for (Map<String, Object> map : memberGender) {

            setmealNames.add(String.valueOf(map.get("name")));
        }
        //构造返回值
        Map<String, Object> map = new HashMap<>();
        map.put("setmealNames", setmealNames);
        map.put("setmealCount", memberGender);
        return new Result(true, MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS, map);
    }
    @GetMapping("/getmemberago")
    public Result getmemberago() {

        //获取数据
        List<Map<String, Object>> memberGender = memberService.groupByAgo();
        List<Map<String, Object>> memberGender1 = new ArrayList<>();

        log.info(":{}",memberGender);
        List<String> setmealNames = new ArrayList<>();
        for (Map<String, Object> map : memberGender) {
        Map<String, Object> map1 = new HashMap<>();
        map1.put("name",map.get("name1"));
        map1.put("value",map.get("value"));
        memberGender1.add(map1);

            setmealNames.add(String.valueOf(map.get("name1")));
        }
        //构造返回值
        log.info(":{}",memberGender1);
        Map<String, Object> map = new HashMap<>();
        map.put("setmealNames", setmealNames);
        map.put("setmealCount", memberGender1);
        return new Result(true, MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS, map);
    }


}
